(function( $ ){
    var count=3;
    var url_get='http://solidaritychain.com.dev/wsSocial/messages.php';
    var url_post='http://solidaritychain.com.dev/wsSocial/message.php';
    var type=null;
    var data = new Array();
    var result;
    var refreshTime=100000;
                
    this.start = function() {
        getMessages();
        setTimeout("this.start()",refreshTime);
    }
                
    this.setMessage = function () {
        this.query(true);
    }
                
    this.getMessages = function (){
        data['count']=count;
        if(type!=null){
            data['type']=type;
        }
                    
        query(false);
    }
                
    this.progress = function (){
        $('#wait').attr('src','./skins/wsSocial/wait.gif');
    }
    
    this.end = function(){
        $('#wait').attr("src", "./skins/wsSocial/start.png");
    }
    
    this.error = function(message){
                    
    }
                
    this.query = function(usePost){
    
        var xhr; 
    
        try {  
            xhr = new ActiveXObject('Msxml2.XMLHTTP');   
        }
        catch (e) 
        {
            try {
                xhr = new ActiveXObject('Microsoft.XMLHTTP'); 
            }
            catch (e2) 
            {
                try {  
                    xhr = new XMLHttpRequest();  
                }
                catch (e3) {  
                    xhr = false;   
                }
            }
        }
        
        xhr.onreadystatechange = function(){
            if(xhr.readyState  == 4)
            {
                if(xhr.status  == 200) {
                    if(!usePost){
                        result = $.parseJSON(xhr.responseText);
                        displayResult();
                    }
                }
                else {
                    error(xhr.status);
                }
                end();
            }
        }
                    
        if(usePost){
            xhr.open("POST", url_post, true);
        }
        else {
            xhr.open("GET", url_get, true);
        }
                    
        var nef = new FormData();
        for(var key in data){
            nef.append(key, data[key]);
        }
       
        xhr.send(nef);
    }
                
    this.displayResult = function(){
        $('#container').empty();
        var template;
        var messageDate = new Date();
        for(res in result){
            template = $('#message_template').clone();
            messageDate.setTime(result[res].date*1000);
            template.children('#date')[0].innerHTML = formatNumber(messageDate.getDate() ) + '/' + formatNumber(messageDate.getMonth()) + '/' + messageDate.getFullYear() + ' - ' +
            formatNumber(messageDate.getHours())+ ':'+formatNumber(messageDate.getMinutes())+':' + formatNumber(messageDate.getSeconds());
            template.children('#date').attr("class",result[res].type);
            template.children('#photo').attr('src', result[res].photo);
            
            if(result[res].text!="")
                template.children('#contenu')[0].innerHTML = result[res].text;
            else
                template.children('#contenu').attr("style", "display:none;");
            
            if(result[res].like!="")
                template.children('#like')[0].innerHTML = result[res].like;
            else
                template.children('#like').attr("style", "display:none;");
            
            if(result[res].attachement!="")
                template.children('#attachement')[0].innerHTML = result[res].attachement;
            else
                template.children('#attachement').attr("style", "display:none;");
            
            template.removeAttr("style");
            template.attr("class", "ws_message");
            template.attr("id","ws_message_"+res);
                        
            $('#container').append(template);
        }
    }
    
    this.formatNumber = function(number){
        if(number < 10)
            return '0'+number;
        return number;
    }
    
    $.fn.postArticle = function() {
        
        data['title']= $('#title').val();
        data['article']= $('#article').val();
        
        if(data['title']=="" || data['article']==""){
            alert('Veuillez completez le formulaire !');
            return false;
        }
        
        progress();
        setMessage();
    }
                
    this.start();
                
})( jQuery );